package com.sc.ktapplication.common.net.okdown;

import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import okhttp3.Request;

public class RequestCall {

    private OkHttpRequest okHttpRequest;
    private Request request;
    private Call call;
    private OkHttpClient clone;

    public RequestCall(OkHttpRequest request) {
        this.okHttpRequest = request;
    }

    public Call buildCall() {
        this.request = this.generateRequest();
        this.clone = OkDownloader.getInstance().getOkHttpClient().newBuilder()
                .readTimeout(40, TimeUnit.SECONDS)
                .writeTimeout(40, TimeUnit.SECONDS)
                .connectTimeout(40, TimeUnit.SECONDS)
                .build();
        this.call = this.clone.newCall(this.request);
        return this.call;
    }

    private Request generateRequest() {
        return this.okHttpRequest.generateRequest();
    }

    public void execute(FileCallBack callback) {
        this.buildCall();
        OkDownloader.getInstance().execute(this, callback);
    }

    public Call getCall() {
        return this.call;
    }

    public OkHttpRequest getOkHttpRequest() {
        return this.okHttpRequest;
    }

    public void cancel() {
        if (this.call != null) {
            this.call.cancel();
        }
    }
}
